import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import junit.framework.TestCase;

public class AutoPlayerBoardFirstTest extends TestCase
{
	private BoardMaker myMaker;
	private LexiconInterface myLexicon;
	private AutoPlayerBoardFirst myPlayer;
	private Scanner mySmallDictionary;

	/**
	 * Code that is run before each test method (automatically)
	 */
	public void setUp()
	{
		// Open the dictionary
		try
		{
			mySmallDictionary = new Scanner(new File("smalltestwords.txt"));
		}
		catch (FileNotFoundException e)
		{
			System.out.println(e);
			assertTrue(false);
		}

		// Set up the instance variables
		myLexicon = new LexiconTrie();
		myLexicon.load(mySmallDictionary);
		myMaker = new BoardMaker();
		myPlayer = new AutoPlayerBoardFirst(myLexicon);
	}

	/**
	 * Closes smalltestwords.txt after each test.
	 */
	public void tearDown()
	{
		mySmallDictionary.close();
	}

	/**
	 * This tests that we walk along the corners successfully. It is a very basic test- if you pass it, you should not assume that your code will work in all cases.
	 */
	public void testFindAllWords()
	{
		// This is how you can create a test board:
		String[] boardContents = { "qqqq", "qilt", "ated", "duck", };
		ArrayList<String> legalWords = new ArrayList<String>();
		legalWords.add("a");
		legalWords.add("dated");
		legalWords.add("duck");
		legalWords.add("quil");
		legalWords.add("quilt");
		BoggleBoard board = myMaker.makeBoard(boardContents);

		myPlayer.findAllValidWords(board, myLexicon, 4);

		assertEquals(myPlayer.wordCount(), 5);
		Iterator<String> i = myPlayer.iterator();
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertFalse(i.hasNext());

		myLexicon = new LexiconArrayList();
		try
		{
			mySmallDictionary = new Scanner(new File("smalltestwords.txt"));
		}
		catch (FileNotFoundException e)
		{
			System.out.println(e);
			assertTrue(false);
		}
		
		myLexicon.load(mySmallDictionary);
		myPlayer.clear();
		myPlayer.findAllValidWords(board, myLexicon, 4);

		assertEquals(myPlayer.wordCount(), 5);
		i = myPlayer.iterator();
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertTrue(legalWords.contains(i.next()));
		assertFalse(i.hasNext());
	}

	/**
	 * This tests if prefixes/words can be correctly found in the lexicon. This is a catch all test to make sure that the lexicon being used in this test suite is correctly implemented.
	 */
	public void testLexicon()
	{
		assertTrue(myLexicon.containsPrefix("dat"));
		assertTrue(myLexicon.containsPrefix("duc"));
		assertTrue(myLexicon.containsPrefix("gor"));
		assertTrue(myLexicon.containsPrefix("date"));
		assertTrue(myLexicon.containsPrefix("dated"));
		assertTrue(myLexicon.containsPrefix("quil"));
		assertTrue(myLexicon.containsPrefix("quilt"));
		assertFalse(myLexicon.contains("du"));
		assertTrue(myLexicon.contains("a"));
		assertTrue(myLexicon.contains("big"));
		assertTrue(myLexicon.contains("cat"));
		assertTrue(myLexicon.contains("dated"));
		assertTrue(myLexicon.contains("duck"));
		assertTrue(myLexicon.contains("every"));
		assertTrue(myLexicon.contains("fat"));
		assertTrue(myLexicon.contains("gore"));
		assertTrue(myLexicon.contains("gorilla"));
		assertTrue(myLexicon.contains("quilt"));
		assertTrue(myLexicon.contains("quil"));
	}

	/**
	 * This tests if myPlayer has correctly loaded the lexicon
	 */
	public void testHasLexicon()
	{
		((AutoPlayerBoardFirst) myPlayer).hasLexicon();
	}
}